<template>
	<div class="dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :close-on-click-modal="false" width="500px">
			<el-form ref="formRef" :model="state.ruleForm" :rules="state.rules">
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="分类名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入分类名称" clearable />
						</el-form-item>
					</el-col>
				</el-row>
				<!-- <el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="分类等级" prop="level">
							<el-radio-group v-model="state.ruleForm.level" @change="onLevelChange">
								<el-radio :value="1">一级</el-radio>
								<el-radio :value="2">二级</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row> -->
				<!-- <el-row v-if="state.ruleForm.level === 2" class="mb20">
					<el-col :span="24">
						<el-form-item label="所属上级" prop="pid">
							<el-select
								v-model="state.ruleForm.pid"
								clearable
								placeholder="请输入关键字/选择所属上级"
								filterable
								remote
								:remote-method="pIdRemoteMethod"
								remote-show-suffix
								:loading="state.pIdSelect.loading"
								@focus="onPidFocus"
							>
								<el-option v-for="item in state.pIdSelect.options" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row> -->
				<el-row :gutter="20">
					<el-col :span="24">
						<el-form-item label="排序" prop="display_order">
							<el-input v-model="state.ruleForm.display_order" placeholder="请输入排序" clearable @input="onVerifiyNumberIntegerDisplayOrder" />
						</el-form-item>
					</el-col>
				</el-row>
				<!-- <el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="状态" prop="status">
							<el-radio-group v-model="state.ruleForm.status">
								<el-radio :value="1">启用</el-radio>
								<el-radio :value="0">禁用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row> -->
			</el-form>
			<template #footer>
				<el-button @click="onCancel" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.dialog.submitLoading" size="default">提 交</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { nextTick, reactive, ref } from 'vue';
import { goodsClassApi } from '/@/api';
import { verifiyNumberInteger } from '/@/utils/toolsValidate';

const GoodsClassApi = goodsClassApi();
let callback: Function = () => {};
// 定义变量内容
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	},
	ruleForm: {
		goods_class_id: '',
		name: '',
		level: 1,
		pid: '',
		display_order: '',
	},
	rules: {
		name: [{ required: true, message: '分类名称不能为空', trigger: ['blur'] }],
		// pid: [{ required: true, message: '所属上级不能为空', trigger: ['blur', 'change'] }],
	},
	pIdSelect: {
		loading: false,
		options: [] as any,
	},
});
const formRef = ref();

/********** 触发事件 **********/
// // 切换分类等级
// const onLevelChange = (val: number) => {
// 	if (val === 1) {
// 		state.ruleForm.pid = '';
// 	}
// };
// // 所属上级获取焦点
// const onPidFocus = () => {
// 	if (state.ruleForm.pid) return;
// 	_goodsClassList({ pageNum: 1, pageSize: 10 });
// };
// 排序过滤
const onVerifiyNumberIntegerDisplayOrder = (val: string) => {
	state.ruleForm.display_order = verifiyNumberInteger(val);
};
// 取消
const onCancel = () => {
	state.dialog = {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	};
};
// 提交
const onSubmit = () => {
	formRef.value.validate((valid: boolean) => {
		if (!valid) return;
		state.dialog.submitLoading = true
		if (state.dialog.type === 'add') {
			_goodsClassAdd({
				...state.ruleForm,
				pid: state.ruleForm.level === 1 ? 0 : state.ruleForm.pid,
			});
		} else {
			_goodsClassEdit({
				...state.ruleForm,
				pid: state.ruleForm.level === 1 ? 0 : state.ruleForm.pid,
			});
		}
	});
};

/********** 方法事件 **********/
// 打开
const openDialog = (data: any, row: any, fn: Function) => {
	state.dialog = {
		isShowDialog: true,
		type: data.type,
		title: data.title,
		submitLoading: false,
	};
	callback = fn;
	nextTick(() => {
		formRef.value.resetFields();
		if (state.dialog.type !== 'add') {
			_goodsClassInfo(row);
		}
	});
};
// // 所属上级搜索选择
// const pIdRemoteMethod = (query: string) => {
// 	if (!query) return;
// 	_goodsClassList({ pageNum: 1, pageSize: 10, name: query });
// };

/********** 网络请求 **********/
// // 商品分类列表
// const _goodsClassList = (params: Object) => {
// 	state.pIdSelect.loading = true;
// 	GoodsClassApi.goodsClassList(params)
// 		.then((res) => {
// 			state.pIdSelect.options = res.data.list;
// 			state.pIdSelect.loading = false;
// 		})
// 		.catch(() => (state.pIdSelect.loading = false));
// };
// 获取商品分类信息
const _goodsClassInfo = (params: any) => {
	GoodsClassApi.goodsClassInfo({ goods_class_id: params.id }).then((res) => {
		state.ruleForm = {
			...res.data.info,
			level: res.data.info.pid ? 2 : 1,
			goods_class_id: res.data.info.id,
		};
		if (res.data.info.pid) {
			state.pIdSelect.options = [{ id: res.data.info.pid, name: params.pname }];
		}
	});
};
// 添加商品分类
const _goodsClassAdd = (params: Object) => {
	GoodsClassApi.goodsClassAdd(params)
		.then(() => {
			ElMessage.success('操作成功！');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};
// 编辑商品分类
const _goodsClassEdit = (params: Object) => {
	GoodsClassApi.goodsClassEdit(params)
		.then(() => {
			ElMessage.success('操作成功！');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};

/********** 方法向父组件暴露 **********/
defineExpose({ openDialog });
</script>
